# -*- coding: utf-8 -*-
# @Time : 2025/1/23 16:58
# @Author : debbyszhang
# @File : conftest.py
# @desc :
import os

import pytest
from playwright.sync_api import sync_playwright

from config import settings
from libs.tools.utils import set_permanent_env_variable


@pytest.fixture(scope="session", autouse=True)
def page():
    with sync_playwright() as p:
        # browser = p.chromium.launch(headless=True) # 静默运行
        browser = p.chromium.launch(headless=settings.HEAD_LESS)
        context = browser.new_context()
        page = context.new_page()
        # login_page = LoginPage(page)
        # login_page.login(settings.USER_CROSS, settings.USER_CROSS_PASSWORD)
        yield page
        # 后置钩子：关闭浏览器
        # time.sleep(1)
        page.close()
        context.close()
        browser.close()


@pytest.fixture(scope="session", autouse=True)
def env(request):
    return request.config.getoption("--env")


def pytest_addoption(parser):
    """
    注册自定义参数命令行参数
    """
    parser.addoption("--env", default="test", action="store", choices=['jenkins', 'test', 'pre'],
                     help="命令行参数 '--env' 设置环境切换")


def pytest_sessionstart(session):
    env = session.config.getoption("--env")
    os.environ["MYAPP_ENV"] = env  # 设置环境变量
    # set_permanent_env_variable("MYAPP_ENV", env)
    # os.environ["MYAPP_ENV"] = env



# pip install python_jenkins
